﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using System.Data;
using Microsoft.Reporting.WebForms;
using SIPO.Web.Support;

public partial class reports_Transaction_rptPrintBilling : System.Web.UI.Page
{
    private recBillingRepo recBillingDal = new recBillingRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
  
    private string parameters = string.Empty;
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
                DateTime? sPeriodeAwal = Context.Request.Params["periodeAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAwal"], Helper.Culture.Indonesian);
                DateTime? sPeriodeAkhir = Context.Request.Params["periodeAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAkhir"], Helper.Culture.Indonesian);
                DateTime? sDueDateAwal = Context.Request.Params["dueDateAwal"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["DueDateAwal"], Helper.Culture.Indonesian);
                DateTime? sDueDateAkhir = Context.Request.Params["dueDateAkhir"].Equals(string.Empty) ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["DueDateAkhir"], Helper.Culture.Indonesian);
                string transTypeID = Context.Request.Params["transTypeID"] == null ? string.Empty : Context.Request.Params["transTypeID"];
                string debtorSectorID = Context.Request.Params["debtorSectorID"] == null ? string.Empty : Context.Request.Params["debtorSectorID"];
                string balance = Context.Request.Params["balance"] == null ? string.Empty : Context.Request.Params["balance"];
                string userID = Context.Request.Params["userID"] == null ? string.Empty : Context.Request.Params["userID"].ToString();
                

                //konversi tanggal
                string periodeAwal = string.Format("{0:yyyyMMdd}", sPeriodeAwal);
                string periodeAkhir = string.Format("{0:yyyyMMdd}", sPeriodeAkhir);
                string dueDateAwal = string.Format("{0:yyyyMMdd}", sDueDateAwal);
                string dueDateAkhir = string.Format("{0:yyyyMMdd}", sDueDateAkhir);

                parameters += "WHERE recUserSector.UserID = ISNULL('" + userID + "', recUserSector.UserID)";

                //menngecek data balance
                if (balance.Equals('1'))
                {
                    parameters += " AND recBilling.Balance = recBilling.Value";
                }
                else if (balance.Equals('2'))
                {
                    parameters += " AND recBilling.Balance < recBilling.Value";
                }

                if (!periodeAkhir.Equals(string.Empty) && !periodeAwal.Equals(string.Empty))
                {
                    parameters += " AND recBilling.BillingDate BETWEEN ISNULL ('" + periodeAwal + "', recBilling.BillingDate) AND ISNULL ('" + periodeAkhir + "', recBilling.BillingDate)";
                }

                if (!dueDateAwal.Equals(string.Empty) && !dueDateAkhir.Equals(string.Empty))
                {
                    parameters += " AND recBilling.DueDate BETWEEN ISNULL ('" + dueDateAwal + "', recBilling.DueDate) AND ISNULL ('" + dueDateAkhir + "', recBilling.DueDate)";
                }

                //mengecek data transTypeID
                if (!transTypeID.Equals(string.Empty))
                {
                    parameters += " AND recBilling.TransTypeID = ISNULL ('" + transTypeID + "', recBilling.TransTypeID)";
                }

                //mengecek data debtorSectorID    
                if (!debtorSectorID.Equals(string.Empty))
                {
                    parameters += " AND recBilling.DebtorSectorID = ISNULL ('" + debtorSectorID + "', recBilling.DebtorSectorID)";
                }

             try
             {
                DataTable dt = recBillingDal.GetListReport(parameters);

                rsViewer.Reset();
                rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/Transaction/rptBilling.rdlc");
                ReportDataSource rds = new ReportDataSource();
                rds.Name = "DataSet1";
                rds.Value = dt;

                rsViewer.LocalReport.DataSources.Clear();
                rsViewer.LocalReport.DataSources.Add(rds);
                rsViewer.LocalReport.Refresh();
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}